package com.hunau.system.service.impl;

import java.util.List;

import com.hunau.common.exception.BusinessException;
import com.hunau.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hunau.system.mapper.TbEnergyMapper;
import com.hunau.system.domain.TbEnergy;
import com.hunau.system.service.ITbEnergyService;
import com.hunau.common.core.text.Convert;

/**
 * 能源数据采集Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-03-17
 */
@Service
public class TbEnergyServiceImpl implements ITbEnergyService 
{
    private static final Logger log = LoggerFactory.getLogger(TbEnergyServiceImpl.class);
    @Autowired
    private TbEnergyMapper tbEnergyMapper;

    /**
     * 查询能源数据采集
     * 
     * @param id 能源数据采集ID
     * @return 能源数据采集
     */
    @Override
    public TbEnergy selectTbEnergyById(Long id)
    {
        return tbEnergyMapper.selectTbEnergyById(id);
    }

    /**
     * 查询能源数据采集列表
     * 
     * @param tbEnergy 能源数据采集
     * @return 能源数据采集
     */
    @Override
    public List<TbEnergy> selectTbEnergyList(TbEnergy tbEnergy)
    {
        return tbEnergyMapper.selectTbEnergyList(tbEnergy);
    }

    /**
     * 新增能源数据采集
     * 
     * @param tbEnergy 能源数据采集
     * @return 结果
     */
    @Override
    public int insertTbEnergy(TbEnergy tbEnergy)
    {
        return tbEnergyMapper.insertTbEnergy(tbEnergy);
    }

    /**
     * 修改能源数据采集
     * 
     * @param tbEnergy 能源数据采集
     * @return 结果
     */
    @Override
    public int updateTbEnergy(TbEnergy tbEnergy)
    {
        return tbEnergyMapper.updateTbEnergy(tbEnergy);
    }

    /**
     * 删除能源数据采集对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTbEnergyByIds(String ids)
    {
        return tbEnergyMapper.deleteTbEnergyByIds(Convert.toStrArray(ids));
    }
    @Override
    public String importUser(List<TbEnergy> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (TbEnergy user : userList) {
            try {
                // 验证是否存在这个用户
                TbEnergy u = tbEnergyMapper.selectTbEnergyByMid(user.getImei());
                if (StringUtils.isNull(u)) {
                    this.insertTbEnergy(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、MIM " + user.getImei() + " 导入成功");
                } else if (isUpdateSupport) {
                    this.updateTbEnergy(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、MIM " + user.getImei() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、MIM " + user.getImei() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、MIM " + user.getImei() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<TbEnergy> selectTbEnergyListByIds(List<Object> array) {
        return tbEnergyMapper.selectTbEnergyListByIds(array);
    }

    /**
     * 删除能源数据采集信息
     * 
     * @param id 能源数据采集ID
     * @return 结果
     */
    @Override
    public int deleteTbEnergyById(Long id)
    {
        return tbEnergyMapper.deleteTbEnergyById(id);
    }
}
